using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using Weight.Base;

namespace JHVideo
{
    class ImageHandler : IImageHandler
    {
        /// <summary>
        /// 压缩图片
        /// </summary>
        /// <param name="path">路径</param>
        /// <param name="ratio">压缩比例</param>
        /// <returns></returns>
        public ResultEntity CompressImg(string path, int ratio)
        {
            ResultEntity res = ResultEntity.Default();
            if (!File.Exists(path)) return  ResultEntity.Error("指定路径的文件不存在，压缩失败！");
            
            try
            {
                using (MemoryStream stream = new MemoryStream())
                {
                    byte[] buffer = new byte[1024];
                    FileStream fRead = new FileStream(path, FileMode.Open, FileAccess.Read);
                    int len = 0;
                    while ((len = fRead.Read(buffer, 0, buffer.Length)) > 0)
                    {
                        stream.Write(buffer, 0, len);
                    }
                    fRead.Close();
                    using (Image resourImg = Image.FromStream(stream))
                    {
                        int width = resourImg.Width * Convert.ToInt32(ratio) / 100;
                        int height = resourImg.Height * Convert.ToInt32(ratio) / 100;
                        using (Bitmap bitmap = new Bitmap(resourImg, width, height))
                        {
                            bitmap.Save(path);
                            res = ResultEntity.Success("压缩成功！");
                        }
                    }
                }
            }
            catch(Exception ex)
            {
                res = ResultEntity.Error("压缩图片异常:"+ex.Message+ex.StackTrace);
            }

            return res;
        }

       /// <summary>
       /// 绘制字符串到图片
       /// </summary>
       /// <param name="para"></param>
       /// <returns></returns>
        public ResultEntity DrawString(DrawStringParam para)
        {
            ResultEntity res = ResultEntity.Default();
            if(string.IsNullOrEmpty(para.Path))
            {
                return ResultEntity.Error("绘制字符串的文件路径不正确,绘制失败,路径:"+para.Path);
            }

            if(!File.Exists(para.Path))
            {
                return ResultEntity.Error("绘制字符串的文件不存在,绘制失败,路径:" + para.Path);
            }
            FileStream stream = null;
            MemoryStream ms = new MemoryStream(2048);
            try
            {
                //读取文件流到内存
                stream = new FileStream(para.Path, FileMode.Open, FileAccess.Read);
                byte[] buf = new byte[1024];
                int len = 0;
                while ((len = stream.Read(buf, 0, buf.Length)) > 0)
                {
                    ms.Write(buf, 0, len);
                }
                stream.Close();

                //生成画布开始绘制文字内容
                using (Image img = Bitmap.FromStream(ms))
                {
                    using (Graphics g = Graphics.FromImage(img))
                    {
                        Font font = new Font("宋体", para.FontSize, FontStyle.Bold);
                        Brush brush = new SolidBrush(Color.FromArgb(220, 255, 0, 0));
                        int y = para.Y;
                        string content = string.Empty;
                        for (int index = 0; index < para.Values.Count; index++)
                        {
                            content = para.Values[index];
                            if (string.IsNullOrEmpty(content)) continue;
                            PointF p = new PointF(para.X, y);
                            g.DrawString(content, font, brush, p);
                            y = y + font.Height + para.LineSpace;
                        }
                        img.Save(para.Path);
                        res = ResultEntity.Success("绘制成功");
                    }
                }
            }
            catch (Exception ex)
            {
                res = ResultEntity.Error("绘制异常:"+ex.Message+ex.StackTrace);
            }
            finally
            {
                ms.Close();
            }

            return res;
        }
    }
}


